package my.myaction.tool.core;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-4-13
 * Time: 10:08:37
 * To change this template use File | Settings | File Templates.
 */
public class MyActionFilter implements Filter {

    private String requestencode = "utf-8";
    private String responseencode = "utf-8";

    public void init(FilterConfig filterConfig) throws ServletException {
        String requestencoding = filterConfig.getInitParameter("RequestEncoding");
        String responseencoding = filterConfig.getInitParameter("ResponseEncoding");
        if(requestencoding != null) {
            this.requestencode = requestencoding.trim();
        }
        if(responseencoding != null) {
            this.responseencode = responseencoding.trim();
        }
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try {
            request = new MyHttpRequest(request);
            response = new MyHttpResponse(request, response);
            JSPContext.put(request, response);

            //设置编码
            request.setCharacterEncoding(requestencode);
            response.setCharacterEncoding(responseencode);
            String Ajax_Response_Forward_Content = request.getParameter("Ajax_Response_Forward");
            if(Ajax_Response_Forward_Content != null) {
                response.getWriter().write(Ajax_Response_Forward_Content);
                return;
            }
            filterChain.doFilter(request, response);
        } catch(Exception ex) {
            ex.printStackTrace();
            if("0".equals(request.getHeader("Ajax_Redirect_Type"))) {//Ajax请求且自动处理重定向
                response.getWriter().write("对不起，发生了不可预知的异常,请检查您的输入或联系相关人员");
                return;
            } else {
                throw new ServletException("发生了异常");
            }
        }
    }

    public void destroy() {

    }


    public static void main(String[] args) throws UnsupportedEncodingException {
        System.out.println(HttpServletRequest.class.getCanonicalName());
    }
}
